const path = require('path');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');

module.exports = {
  devServer: {
    port: 8000,
    proxy: {
      '/api': 'http://localhost:3001',
    },
    // 修复弃用警告 - 使用新的配置方式
    client: {
      overlay: {
        errors: true,
        warnings: false,
      },
    },
    // 禁用弃用的选项
    onAfterSetupMiddleware: undefined,
    onBeforeSetupMiddleware: undefined,
  },
  webpack: {
    configure: (webpackConfig) => {
      // 只在生产模式下配置代码分割
      if (webpackConfig.mode === 'production') {
        // 确保 optimization 对象存在
        if (webpackConfig.optimization == null) {
          webpackConfig.optimization = {};
        }
        
        // 配置代码分割 - 只在生产模式下生效
        webpackConfig.optimization.splitChunks = {
          chunks: 'all',
          minSize: 20000,
          maxSize: 244000,
          cacheGroups: {
            antd: {
              name: 'antd-chunk',
              test: /[\\/]node_modules[\\/]antd[\\/]/,
              priority: 100,
              reuseExistingChunk: true,
            },
            reactDom: {
              name: 'react-dom-chunk',
              test: /[\\/]node_modules[\\/](react-dom|react)[\\/]/,
              priority: 99,
              reuseExistingChunk: true,
            },
            redux: {
              name: 'redux-chunk',
              test: /[\\/]node_modules[\\/](@reduxjs|redux)[\\/]/,
              priority: 95,
              reuseExistingChunk: true,
            },
            charts: {
              name: 'charts-chunk',
              test: /[\\/]node_modules[\\/](recharts)[\\/]/,
              priority: 90,
              reuseExistingChunk: true,
            },
            vendors: {
              name: 'vendors-chunk',
              test: /[\\/]node_modules[\\/]/,
              priority: 80,
              reuseExistingChunk: true,
            },
          },
        };
      }
      
      // 开发环境使用更快的 source map
      if (webpackConfig.mode === 'development') {
        webpackConfig.devtool = 'eval-cheap-module-source-map';
      } else {
        webpackConfig.devtool = 'source-map';
      }

      // 添加 BundleAnalyzerPlugin
      if (process.env.ANALYZE === 'true') {
        webpackConfig.plugins.push(
          new BundleAnalyzerPlugin({
            analyzerMode: 'server',
            openAnalyzer: true,
            analyzerPort: 8889,
          })
        );
      }

      // 移除所有 source-map-loader 规则
      webpackConfig.module.rules = webpackConfig.module.rules.filter((rule) => {
        if (rule.use && Array.isArray(rule.use)) {
          const hasSourceMapLoader = rule.use.some(
            (use) => use.loader && use.loader.includes('source-map-loader')
          );
          return !hasSourceMapLoader;
        }
        return true;
      });

      // 添加忽略 source map 警告的配置
      webpackConfig.ignoreWarnings = [
        /Failed to parse source map/,
        /source-map-loader/,
        /The legacy JS API is deprecated/,
      ];

      // 配置路径别名
      webpackConfig.resolve.alias = {
        ...webpackConfig.resolve.alias,
        '@': path.resolve(__dirname, 'src'),
        '@/components': path.resolve(__dirname, 'src/components'),
        '@/components/ui': path.resolve(__dirname, 'src/components/ui'),
        '@/components/business': path.resolve(
          __dirname,
          'src/components/business'
        ),
        '@/components/question': path.resolve(
          __dirname,
          'src/components/question'
        ),
        '@/pages': path.resolve(__dirname, 'src/pages'),
        '@/hooks': path.resolve(__dirname, 'src/hooks'),
        '@/services': path.resolve(__dirname, 'src/services'),
        '@/store': path.resolve(__dirname, 'src/store'),
        '@/utils': path.resolve(__dirname, 'src/utils'),
        '@/constant': path.resolve(__dirname, 'src/constant'),
        '@/Layout': path.resolve(__dirname, 'src/Layout'),
        '@/router': path.resolve(__dirname, 'src/router'),
        '@/assets': path.resolve(__dirname, 'src/assets'),
      };

      return webpackConfig;
    },
  },
};
